using System.Collections;
using System.Collections.Generic;
using UnityEngine;


/// <summary>
/// 任务系统
/// </summary>
public class QuestSystem
{
    public static QuestSystem Instance { get; } = new QuestSystem();

    public Queue<QuestBase> Quests { get; set; }

    public QuestBase CurrentQuest { get; set; } = null;

    public List<QuestBase> ArchivedQuests { get; set;} = new List<QuestBase>();

    public QuestSystem()
    {
        // Test
        PlayerPrefs.DeleteAll();

        this.Quests = new Queue<QuestBase>();
        // 任务一：系统任务
        var systemQuest = new SystemQuest()
        {
            Name = nameof(SystemQuest),
            Giver = "System",
            Status = QuestStatus.Available,
            BeforeDialogs = new(),
            AcceptIsCompleted = true
        };
        systemQuest.BeforeDialogs.Enqueue(new() { CharacterName = Defines.MainCharacterName, Content = "Hello，我是Luna，你可以用上下左右键控制我移动，请按空格键继续" });
        this.Quests.Enqueue(systemQuest);
        // 任务二：安抚小狗
        var sootheDog = new SootheDog()
        {
            Name = nameof(SootheDog),
            Giver = Defines.Nala,
            BeforeDialogs = new(),
            DuringDialogs = new()
        };
        sootheDog.BeforeDialogs.Enqueue(new() { CharacterName = Defines.Nala, Content = "好久不见，小猫咪Luna～" });
        sootheDog.BeforeDialogs.Enqueue(new() { CharacterName = Defines.MainCharacterName, Content = "好久不见Nala，你还是那么有活力，哈哈" });
        sootheDog.BeforeDialogs.Enqueue(new() { CharacterName = Defines.Nala, Content = "还好吧～" });
        sootheDog.BeforeDialogs.Enqueue(new() { CharacterName = Defines.Nala, Content = "我的狗一直在叫，但是我现在忙不过来，你能帮我安抚一下吗" });
        sootheDog.BeforeDialogs.Enqueue(new() { CharacterName = Defines.MainCharacterName, Content = "啊？" });
        sootheDog.BeforeDialogs.Enqueue(new() { CharacterName = Defines.Nala, Content = "别看它叫得这么凶，其实它就是想引起别人的注意" });
        sootheDog.BeforeDialogs.Enqueue(new() { CharacterName = Defines.MainCharacterName, Content = "可是..." });
        sootheDog.BeforeDialogs.Enqueue(new() { CharacterName = Defines.MainCharacterName, Content = "我是猫女郎啊" });
        sootheDog.BeforeDialogs.Enqueue(new() { CharacterName = Defines.Nala, Content = "安心啦，不会咬你哒，去吧去吧～" });
        sootheDog.BeforeDialogs.Enqueue(new() { CharacterName = Defines.Nala, Content = "摸摸它就行" });
        sootheDog.DuringDialogs.Add(new() { CharacterName = Defines.Nala, Content = "它还在叫呢" });
        this.Quests.Enqueue(sootheDog);
        // 任务三：收集蜡烛
        var collectCandles = new CollectCandles()
        {
            Name = nameof(CollectCandles),
            Giver = Defines.Nala,
            BeforeDialogs = new(),
            DuringDialogs = new()
        };
        collectCandles.BeforeDialogs.Enqueue(new() { CharacterName = Defines.Nala, Content = "感谢你呐，Luna，你还是那么可靠！" });
        collectCandles.BeforeDialogs.Enqueue(new() { CharacterName = Defines.Nala, Content = "我想请你帮个忙好吗" });
        collectCandles.BeforeDialogs.Enqueue(new() { CharacterName = Defines.Nala, Content = "说起来这事怪我..." });
        collectCandles.BeforeDialogs.Enqueue(new() { CharacterName = Defines.Nala, Content = "今天我睡过头了，出门比较匆忙" });
        collectCandles.BeforeDialogs.Enqueue(new() { CharacterName = Defines.Nala, Content = "然后装蜡烛的袋子没封好" });
        collectCandles.BeforeDialogs.Enqueue(new() { CharacterName = Defines.Nala, Content = "结果就...蜡烛基本都丢完了" });
        collectCandles.BeforeDialogs.Enqueue(new() { CharacterName = Defines.MainCharacterName, Content = "你还是老样子，哈哈..." });
        collectCandles.BeforeDialogs.Enqueue(new() { CharacterName = Defines.Nala, Content = "所以，你帮帮忙，帮我把蜡烛找回来" });
        collectCandles.BeforeDialogs.Enqueue(new() { CharacterName = Defines.Nala, Content = "如果你能帮我找回全部的5根蜡烛，我就送你一把神器" });
        collectCandles.BeforeDialogs.Enqueue(new() { CharacterName = Defines.MainCharacterName, Content = "神器？" });
        collectCandles.BeforeDialogs.Enqueue(new() { CharacterName = Defines.Nala, Content = "是的，我感觉很适合你，加油呐～" });
        collectCandles.DuringDialogs.Add(new() { CharacterName = Defines.Nala, Content = "你还没帮我收集到所有的蜡烛，宝～" });
        this.Quests.Enqueue(collectCandles);
        // 任务四：杀怪物
        var killMonsters = new KillMonsters()
        {
            Name = nameof(KillMonsters),
            Giver = Defines.Nala,
            BeforeDialogs = new(),
            DuringDialogs = new(),
            AfterDialogs = new()
        };
        killMonsters.BeforeDialogs.Enqueue(new() { CharacterName = Defines.Nala, Content = "可靠啊，竟然一个不差全收集回来了" });
        killMonsters.BeforeDialogs.Enqueue(new() { CharacterName = Defines.MainCharacterName, Content = "你知道多累吗" });
        killMonsters.BeforeDialogs.Enqueue(new() { CharacterName = Defines.MainCharacterName, Content = "你到处跑，真的很难收集" });
        killMonsters.BeforeDialogs.Enqueue(new() { CharacterName = Defines.Nala, Content = "辛苦啦辛苦啦" });
        killMonsters.BeforeDialogs.Enqueue(new() { CharacterName = Defines.Nala, Content = "这是给你的奖励" });
        killMonsters.BeforeDialogs.Enqueue(new() { CharacterName = Defines.Nala, Content = "救赎之锤，传说中的神器" });
        killMonsters.BeforeDialogs.Enqueue(new() { CharacterName = Defines.Nala, Content = "应该挺适合你的" });
        killMonsters.BeforeDialogs.Enqueue(new() { CharacterName = Defines.MainCharacterName, Content = "哇，谢谢你！" });
        killMonsters.BeforeDialogs.Enqueue(new() { CharacterName = Defines.Nala, Content = "嘿嘿，咱们的关系不用客气" });
        killMonsters.BeforeDialogs.Enqueue(new() { CharacterName = Defines.Nala, Content = "正好，最近山里出现几个怪物，你也算是为民除害，帮忙清理一下" });
        killMonsters.BeforeDialogs.Enqueue(new() { CharacterName = Defines.MainCharacterName, Content = "啊？" });
        killMonsters.BeforeDialogs.Enqueue(new() { CharacterName = Defines.MainCharacterName, Content = "这才是你的真实目的吧" });
        killMonsters.BeforeDialogs.Enqueue(new() { CharacterName = Defines.Nala, Content = "拜托拜托啦，否则真的不方便我卖东西" });
        killMonsters.BeforeDialogs.Enqueue(new() { CharacterName = Defines.MainCharacterName, Content = "（无语中...）" });
        killMonsters.BeforeDialogs.Enqueue(new() { CharacterName = Defines.Nala, Content = "求求你了" });
        killMonsters.BeforeDialogs.Enqueue(new() { CharacterName = Defines.MainCharacterName, Content = "哎，行吧" });
        killMonsters.BeforeDialogs.Enqueue(new() { CharacterName = Defines.Nala, Content = "嘻嘻，那辛苦宝子啦" });
        killMonsters.DuringDialogs.Add(new() { CharacterName = Defines.Nala, Content = "宝，你还没清理干净呢，这样我不方便嘛～" });
        killMonsters.AfterDialogs.Enqueue(new() { CharacterName = Defines.Nala, Content = "真棒，Luna，周围的居民都会十分感谢你的，有机会来我家喝一杯" });
        killMonsters.AfterDialogs.Enqueue(new() { CharacterName = Defines.MainCharacterName, Content = "我觉得可行，哈哈～" });
        this.Quests.Enqueue(killMonsters);

        this.LoadCurrentQuest();
        GameManager.Instance.QuestLoaded = true;
    }

    public void GetQuest()
    {
        if (this.CurrentQuest != null)
        {
            this.ArchivedQuests.Add(this.CurrentQuest);
        }

        if (this.Quests.TryDequeue(out var currentQuest))
        {
            Debug.Log($"Quest Name: {currentQuest.Name}");
            this.SetCurrentQuest(currentQuest);
        }
        else
        {
            this.CurrentQuest = null;
            PlayerPrefs.SetString(Defines.QuestName, "");
        }
    }

    private void SetCurrentQuest(QuestBase currentQuest)
    {
        this.CurrentQuest = currentQuest;
        if (currentQuest.ActiveMode == ActiveMode.Auto)
        {
            this.CurrentQuest.Status = QuestStatus.Available;
        }
        PlayerPrefs.SetString(Defines.QuestName, this.CurrentQuest.Name);
    }

    private void LoadCurrentQuest()
    {
        var questName = PlayerPrefs.GetString(Defines.QuestName, "");
        if (!string.IsNullOrWhiteSpace(questName))
        {
            while (this.Quests.TryDequeue(out var currentQuest))
            {
                if (questName == currentQuest.Name)
                {
                    SetCurrentQuest(currentQuest);
                    break;
                }

                /* 处理已经做过的任务 */

                switch (currentQuest.Name)
                {
                    case nameof(CollectCandles):
                        GameManager.Instance.CandlesVisible = false;
                        break;
                }
                currentQuest.Status = QuestStatus.Archived;
                this.ArchivedQuests.Add(currentQuest);
            }
        }
    }

}